$(function () {
  gettags();
  getcategories();
})
$('.toggle').toggles({
    on: true,
    text: {
        on: '开启',
        off: '关闭'
    }
});
$(".select2").select2({
    width: '100%'
});
var tale = new $.tale();
/**
 * 保存文章
 * @param status
 */
function subArticle(status) {
    var title = $('#articleForm input[name=title]').val();
    var content = $('#text').val();
    var categories=$("#categories").val();
    var desciption=$("#description").val();
    if (title == '') {
        tale.alertWarn('请输入文章标题');
        return;
    }
    if (desciption == '') {
        tale.alertWarn('请输入文章描述');
        return;
    }
    if (categories == '') {
        tale.alertWarn('请选择文章分类');
        return;
    }
    if (content == '') {
        tale.alertWarn('请输入文章内容');
        return;
    }
    var on = $("#allow").find('.toggle-on.active').length;
    if(on==1)
        $("#allow_comment").val(1);
    else
        $("#allow_comment").val(0);
    $('#content-editor').val(content);
    $("#status").val(status);
    var params = $("#articleForm").serialize();
    $.post({
        data:params,
        xhrFields: {
            withCredentials: true
        },
        url:baseurl+api.article.publish,
        success: function (result) {
            if (result.code == '200') {
                tale.alertOk({
                    text:'文章保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href="/admin/article/manage";
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.msg || '保存文章失败');
            }
        }
    });
}
$('div.allow-false').toggles({
    off: true,
    text: {
        on: '开启',
        off: '关闭'
    }
});
function gettags() {
   $.post({
       url: baseurl+api.tags.getall,
       xhrFields: {
           withCredentials: true
       },
       success:function (res) {
           var s="";
           for (var i = 0; i < res.data.length; i++) {
               s+="<option value=\""+res.data[i].id+"\">"+res.data[i].name+"</option>";
           }
           $("#tags").html(s);
       }
   })
}
function getcategories() {
    $.post({
        url: baseurl+api.categories.getall,
        xhrFields: {
            withCredentials: true
        },
        success:function (res) {
            var s="<option value=\"\" selected=\"selected\">请选择分类</option>";
            for (var i = 0; i < res.data.length; i++) {
                s+="<option value=\""+res.data[i].id+"\">"+res.data[i].name+"</option>";
            }
            $("#categories").html(s);
        }
    })
}

